package com.zsk.shop.controller.gop;

import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.BizExceptionEnum;
import com.zsk.shop.common.base.ServiceException;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.GopMenu;
import com.zsk.shop.model.params.GopMenuAddParam;
import com.zsk.shop.model.params.GopMenuEditParam;
import com.zsk.shop.service.GopMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单控制器
 *
 * @author fengshuonan
 * @Date 2017年2月12日21:59:14
 */
@RestController
@Api(tags = "Gop菜单角色管理")
@RequestMapping("/gop/menu")
public class GopMenuController {

    @Resource
    private GopMenuService menuService;

    /**
     * 新增菜单
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:53 PM
     */
    @ApiOperation("新增菜单")
    @PostMapping(value = "/add.do")
    public BaseResponse add(@RequestBody @Validated GopMenuAddParam param) {
        return this.menuService.addMenu(param);
    }

    /**
     * 修该菜单
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:53 PM
     */
    @ApiOperation("修改菜单")
    @PostMapping(value = "/edit.do")
    public BaseResponse edit(@RequestBody GopMenuEditParam menu) {
        //如果修改了编号，则该菜单的子菜单也要修改对应编号
        this.menuService.updateMenu(menu);
        return BaseResponse.success();
    }

    /**
     * 删除菜单
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:53 PM
     */
    @ApiOperation("删除菜单")
    @GetMapping(value = "/remove.do")
    @ApiImplicitParam(name = "id", value = "id编号")
    public BaseResponse remove(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.menuService.delMenu(id);
        return BaseResponse.success();
    }

    /**
     * 查看菜单
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:53 PM
     */
    @ApiOperation("查看菜单")
    @GetMapping(value = "/view.do")
    @ApiImplicitParam(name = "id", value = "id菜单编号")
    public BaseResponse view(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        GopMenu menu = this.menuService.getById(id);
        return BaseResponse.success(menu);
    }

    @ApiOperation("获取所有菜单列表")
    @PostMapping(value = "/menuTreeList.do")
    public BaseResponse menuTreeList() {
        return this.menuService.menuTreeList();
    }

    /**
     * 获取角色的菜单列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:54 PM
     */
    @ApiOperation("获取角色的菜单列表")
    @GetMapping(value = "/menuTreeListByRoleId.do")
    @ApiImplicitParam(name = "id", value = "角色id编号")
    public BaseResponse menuTreeListByRoleId(Long roleId) {
        List<String> menuUrls = this.menuService.getMenuUrlByRoleId(roleId);
        if (ToolUtil.isEmpty(menuUrls)) {
            return BaseResponse.success(new ArrayList<>());
        } else {
            return this.menuService.menuTreeListByMenuUrls(menuUrls);
        }
    }

}
